Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
user ajax to get data without "onClick function" in javaScript

I've been searching for the example of getting data by using ajax without onclick function but most of the example that i found was within onclick funtion.
Here is the html that i successfully get the data by using Onclick funtion

<!doctype html>
<html lang="en">
<head>
   <meta charset="utf-8" />
   <title>New document</title>

   <script type = 'text/javascript' src='js/jquery-2.2.0.js'></script>

<script type ='text/javascript' src='js/testScript.js'></script>

<?php
include_once("database_conn_getOffers.php");
?>
</head>
<body>
   content goes here
   <aside id="offer" onclick ="loadDoc('getData.php',myFunction)">
        &nbsp; click here
        </aside>
</body>

here is the script

function loadDoc(url, cFunction) {
  var xhttp;
  xhttp=new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      cFunction(this);
    }
  };
  xhttp.open("GET", url, true);
  xhttp.send();
}
function myFunction(xhttp) {
  document.getElementById("offer").innerHTML =
  xhttp.responseText;
}

Now, i wanted get the data without the onclick function.
I want the data show once i open the html.
Can someone provide me some link or gv me an example ??

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

All you need to do is call the function elsewhere for instance you could set it to be called when the window is loaded.

Window.onload = loadDoc('getData.php',myFunction)

about 4 years ago · Santiago Trujillo Relatório

0

You can use

<body onload="loadDoc('getData.php',myFunction);">

about 4 years ago · Santiago Trujillo Relatório

0

A third solution could be to add a new DOMContentLoaded event to send the query after the browser render the page.

It should be something like this :

document.addEventListener('DOMContentLoaded', loadDoc);

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda